package com.sskj.asset.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.asset.bean.AssetAllBean;
import com.sskj.asset.bean.AssetBean;
import com.sskj.asset.bean.AssetListBean;
import com.sskj.asset.http.HttpConfig;
import com.sskj.asset.ui.activity.AssetManageActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import io.reactivex.Flowable;


public class AssetManageActivityPresenter extends BasePresenter<AssetManageActivity> {
    public Flowable<List<AssetListBean>> getData(String p, String size) {
        return OkGo.<HttpData<AssetAllBean>>post(HttpConfig.BASE_URL + HttpConfig.ASEET_LIST)
                .params("p", p)
                .params("size", size)
                .converter(new JsonConverter<HttpData<AssetAllBean>>() {
                })
                .adapt(new FlowableBody<>())
                .map(aseetListBeanHttpData -> aseetListBeanHttpData.getData().getRes().getAsset())
                .flatMap(assetListBeans -> Flowable.fromIterable(assetListBeans))
                .map(assetListBean -> {
                    if (assetListBean.getPname().equals("TWD")) {
                        assetListBean.setPriority(2);
                    } else if (assetListBean.getPname().equals("FEC")) {
                        assetListBean.setPriority(1);
                    } else {
                        assetListBean.setPriority(0);
                    }
                    return assetListBean;
                })
                .sorted((o1, o2) -> o2.getPriority() - o1.getPriority())
                .toList()
                .toFlowable()
                .onErrorReturnItem(new ArrayList());
    }

    public void getTop() {
        OkGo.<HttpData<AssetBean>>post(HttpConfig.BASE_URL + HttpConfig.ASEET_LIST)
                .execute(new JsonCallBack<HttpData<AssetBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<AssetBean>> response) {
                        HttpData<AssetBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData().getRes().getTotal());
                        }
                    }
                });

    }


}
